package com.itheima.d03_map_use;

import java.util.*;
import java.util.function.BiFunction;

/**
 * 目标：完成统计票数的案例
 */
public class Test {

    public static void main(String[] args) {
        //1.模拟测试数据，产生投票数据
        String[] selects = {"大明湖","趵突泉","千佛山","超意新"};
        ArrayList<String> datas = new ArrayList<>(Arrays.asList(
                "13800001111", "13800001111", "13800001112", "13800001113", "13800001114",
                "13800001114", "13800001115", "13800001116", "13800001117", "13800001114",
                "13800001119", "13800001120", "13800001120", "13800001121", "13800001114",
                "13800001123", "13800001124", "13800001125", "13800001126", "13800001114",
                "13800001128", "13800001129", "13800001130", "13800001130", "13800001114",
                "13800001132", "13800001133", "13800001134", "13800001135", "13800001136",
                "13800001137", "13800001138", "13800001139", "13800001140", "13800001141",
                "13800001142", "13800001143", "13800001144", "13800001145", "13800001146",
                "13800001147", "13800001148", "13800001149", "13800001150", "13800001150",
                "13800001151", "13800001152", "13800001153", "13800001154", "13800001155",
                "13800001156", "13800001157", "13800001158", "13800001159", "13800001160",
                "13800001161", "13800001162", "13800001163", "13800001164", "13800001165",
                "13800001166", "13800001167", "13800001168", "13800001169", "13800001170",
                "13800001171", "13800001172", "13800001173", "13800001174", "13800001175",
                "13800001176", "13800001177", "13800001178", "13800001179", "13800001180",
                "13800001181", "13800001182", "13800001183", "13800001184", "13800001185"
        ));

        /*Random r = new Random();
        for (int i = 1; i <= 80; i++) {
            int index = r.nextInt(selects.length);
            datas.add(selects[index]);
        }

        datas.forEach(System.out::println);*/

        //2.统计每个景点的投票人数
        Map<String,Integer> map = new HashMap<>();

        //2.1对80张票机票操作
        for (String v : datas) {
            //查询map中是否有此景点
            if(map.containsKey(v)){
                //2.3如果有，则将此景点的票数 +1
                map.put(v,map.get(v) + 1); //更新票数
            }else{
                //2.2如果没有，存入此景点：（景点名称 = 1）
                map.put(v,1);
            }
        }

        /*for (String v : datas) {
           map.merge(v, 1,(oldValue,one)->oldValue + one);
        }*/

        //3.打印统计结果：map集合遍历
        map.forEach((k,v)-> System.out.println(k + ":" + v));

    }
}








